<template>
  <div class="moment-item">
    <div class="moment-header">
      <div class="user-info-wrapper">
        <head-image :url="moment.userInfo.headImage" :size="40"></head-image>
        <div class="user-info">
          <div class="nickname">{{ moment.userInfo.nickName }}</div>
          <div class="time">{{ formatTime }}</div>
        </div>
      </div>
      <!-- 添加删除按钮 -->
      <div class="delete-btn" v-if="moment.userId === $store.state.userStore.userInfo.id">
        <i class="el-icon-delete" @click="$emit('delete', moment.id)"></i>
      </div>
    </div>

    <div class="moment-content">
      <div class="text">{{ moment.content }}</div>
      <div v-if="moment.images.length" class="images" :class="{'single-image': moment.images.length === 1}">
        <img 
          v-for="(img, index) in moment.images"
          :key="index"
          :src="img"
          @click="previewImage(index)">
      </div>
    </div>

    <div class="moment-footer">
      <div class="actions">
        <span class="like" @click="handleLike">
          <i :class="isLiked ? 'el-icon-star-on' : 'el-icon-star-off'"></i>
          {{ moment.likes.length }}
        </span>
        <span class="comment" @click="toggleCommentInput">
          <i class="el-icon-chat-dot-round"></i>
          {{ moment.comments.length }}
        </span>
      </div>

      <!-- 评论列表 -->
      <comment-list 
        v-if="moment.comments.length > 0"
        :comments="moment.comments"
        @reply="onReply">
      </comment-list>

      <!-- 评论输入框 -->
      <div v-if="showCommentInput" class="comment-input">
        <el-input
          v-model="commentText"
          :placeholder="replyTo ? '回复' + replyUserName : '评论...'"
          @keyup.enter="onSubmitComment"
          @blur="onInputBlur">
          <template slot="append">
            <el-button type="primary" @click="onSubmitComment">发送</el-button>
          </template>
        </el-input>
      </div>
    </div>
  </div>
</template>

<script>
import CommentList from './CommentList.vue'
import HeadImage from '../common/HeadImage.vue'

export default {
  components: {
    CommentList,
    HeadImage
  },
  props: {
    moment: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      showCommentInput: false,
      commentText: '',
      replyTo: null,
      replyUserName: ''
    }
  },
  computed: {
    isLiked() {
      return this.moment.likes.includes(this.$store.state.userStore.userInfo.id)
    },
    formatTime() {
      const date = new Date(this.moment.createTime)
      const now = new Date()
      const diff = now - date
      
      // 小于1分钟
      if (diff < 60 * 1000) {
        return '刚刚'
      }
      // 小于1小时
      if (diff < 60 * 60 * 1000) {
        return Math.floor(diff / (60 * 1000)) + '分钟前'
      }
      // 小于24小时
      if (diff < 24 * 60 * 60 * 1000) {
        return Math.floor(diff / (60 * 60 * 1000)) + '小时前'
      }
      // 小于30天
      if (diff < 30 * 24 * 60 * 60 * 1000) {
        return Math.floor(diff / (24 * 60 * 60 * 1000)) + '天前'
      }
      
      // 超过30天显示具体日期
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      const hours = date.getHours().toString().padStart(2, '0')
      const minutes = date.getMinutes().toString().padStart(2, '0')
      
      if (year === now.getFullYear()) {
        return `${month}月${day}日 ${hours}:${minutes}`
      }
      return `${year}年${month}月${day}日 ${hours}:${minutes}`
    }
  },
  methods: {
    previewImage(index) {
      // 使用 Element UI 的图片预览
      const images = this.moment.images
      const h = this.$createElement
      this.$image({
        url: images[index],
        previewSrcList: images
      })
    },
    toggleCommentInput() {
      this.showCommentInput = !this.showCommentInput
      this.replyTo = null
      this.replyUserName = ''
      this.commentText = ''
      if (this.showCommentInput) {
        this.$nextTick(() => {
          this.$el.querySelector('.comment-input input').focus()
        })
      }
    },
    onReply(userId, nickName) {
      this.replyTo = userId
      this.replyUserName = nickName
      this.showCommentInput = true
      this.$nextTick(() => {
        this.$el.querySelector('.comment-input input').focus()
      })
    },
    onInputBlur() {
      // 使用setTimeout避免点击发送按钮时输入框就消失
      setTimeout(() => {
        if (!this.commentText.trim()) {
          this.showCommentInput = false
        }
      }, 200)
    },
    onSubmitComment() {
      if (!this.commentText.trim()) return
      this.$emit('comment', this.moment.id, this.commentText, this.replyTo)
      this.commentText = ''
      this.showCommentInput = false
      this.replyTo = null
    },
    handleLike() {
      this.$emit('like', this.moment.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.moment-item {
  padding: 20px;
  background: #fff;
  margin-bottom: 15px;
  border-radius: 12px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  
  .moment-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 15px;
    position: relative;
    
    .user-info-wrapper {
      display: flex;
      align-items: flex-start;
      flex: 1;
      min-width: 0;
      
      .user-info {
        margin-left: 12px;
        flex: 1;
        min-width: 0;
        
        .nickname {
          font-size: 16px;
          font-weight: 500;
          color: #333;
          line-height: 1.2;
          margin-bottom: 4px;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
        }
        
        .time {
          font-size: 12px;
          color: #999;
          line-height: 1;
        }
      }
    }
    
    .delete-btn {
      margin-left: 12px;
      flex-shrink: 0;
      color: #999;
      cursor: pointer;
      padding: 5px;
      transition: all 0.3s ease;
      
      &:hover {
        color: #F56C6C;
        transform: scale(1.1);
      }
    }
  }
  
  .moment-content {
    margin: 0 0 15px;
    
    .text {
      font-size: 15px;
      color: #333;
      line-height: 1.6;
      margin-bottom: 12px;
      word-break: break-all;
    }
    
    .images {
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      gap: 8px;
      
      img {
        width: 100%;
        aspect-ratio: 1;
        object-fit: cover;
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:hover {
          opacity: 0.9;
          transform: scale(1.02);
        }
      }
      
      &.single-image img {
        grid-column: span 2;
      }
    }
  }
  
  .moment-footer {
    .actions {
      display: flex;
      justify-content: flex-end;
      padding-bottom: 12px;
      margin-bottom: 12px;
      border-bottom: 1px solid #eee;
      
      span {
        margin-left: 24px;
        color: #666;
        cursor: pointer;
        transition: all 0.3s ease;
        
        &:hover {
          color: var(--theme-color);
        }
        
        i {
          margin-right: 4px;
        }
      }
    }
    
    .comment-list {
      margin-bottom: 12px;
    }
    
    .comment-input {
      margin-top: 12px;
      
      .el-input {
        .el-input__inner {
          border-radius: 20px;
          padding-left: 15px;
        }
        
        .el-input-group__append {
          padding: 0;
          border: none;
          background: transparent;
          
          .el-button {
            border-radius: 0 20px 20px 0;
            padding: 0 20px;
            height: 100%;
          }
        }
      }
    }
  }
}
</style> 